# encoding:utf-8
'''
@author: adog
web: http://www.adog.net.cn
Created on 2020-11-24
'''

import time
import datetime
import os
import platform
import socket
import pandas as pd
# 获取本机电脑�?
pcName = socket.getfqdn(socket.gethostname())

def timeString2timeLong(timeStr,format='day'):
    timeStruc=None
    if format=='sec':
        timeStruc=time.strptime(timeStr, "%Y-%m-%d %H:%M:%S")
    elif format=='min':
        timeStruc=time.strptime(timeStr, "%Y-%m-%d %H:%M")
    elif format=='day':
        timeStruc=time.strptime(timeStr, "%Y-%m-%d")
    timeLong = int(time.mktime(timeStruc))
    return timeLong

def timeLong2Str(timeLong,format='sec'): #timeLong unit: sec
    timeStr=''
    if timeLong is not None:
        #timeLong/=1000
        if format=='day':
            timeStr=time.strftime('%Y-%m-%d', time.localtime(timeLong) )
        elif format=='sec':
            timeStr=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timeLong) )
    return timeStr
    

def nowTime():
    now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    return now


def nowTimeShort():
    now = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    return now


def nowOnlyTime():
    now = time.strftime('%H:%M:%S', time.localtime(time.time()))
    return now


def nowOnlyTimeMins():
    now = time.strftime('%H:%M', time.localtime(time.time()))
    return now


def nowDateMins():
    now = time.strftime('%Y-%m-%d_%H:%M', time.localtime(time.time()))
    return now


def nowDate():
    nowDate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    return nowDate


def getTimeBefAft(timeStr, x_seconds):
    x = time.mktime(time.strptime(timeStr, "%Y-%m-%d %H:%M:%S"))
    y = x + x_seconds
    z = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(y)) 
    return z

def getTimeDiff(timeStr1,timeStr2):
    x1 = time.mktime(time.strptime(timeStr1, "%Y-%m-%d %H:%M:%S"))
    x2 = time.mktime(time.strptime(timeStr1, "%Y-%m-%d %H:%M:%S"))
    diff = x2 - x1
    return  diff.seconds

def getDayDiff(dayStr1,dayStr2):
    x1 = datetime.datetime.strptime(dayStr1, "%Y-%m-%d")
    x2 = datetime.datetime.strptime(dayStr2, "%Y-%m-%d")
    diff = x2 - x1
    return  diff.days


 
    
def adogRootPath():
    sep = ''
    localPath = os.getcwd()
    if platform.uname()[0] == 'Linux':
        sep = '/'
        localPathList = localPath.split(sep)[:6]
    elif platform.uname()[0] == 'Windows':
        sep = '\\'
        localPathList = localPath.split(sep)[:4]
    out = sep.join(localPathList)
    return out


def filePath(filePath):
    path = 'wrong in file path'
    if platform.uname()[0] == 'Linux':
        path = adogRootPath() + path
    elif platform.uname()[0] == 'Windows':
        path = str(adogRootPath() + filePath).replace('/', '\\')
    return path

def code_wxy2jq(code,broker):
    pass

def code_jq2wxy(code,broker=None):
    
    if broker is None:
        cod=code.upper()[:2]
        #XZCE
        if cod in ['AP','CF','CJ','CY','FG','JR','LR','MA','OI','PF','PK','PM','RI','RM','RS','SA','SF','SM','SR','TA','UR','WH','ZC']:
            if len(code)<=7: #fut
                code=code.replace('21','1')
            else: #opt
                pass
        #XINE
        elif cod in ['BC','LU','NR','SC']:
            code=code.lower()
            
    else:
        code=code.upper()
        if broker=='XSGE':
            pass
        elif broker=='XZCE':
            if len(code)<=7: #fut
                code=code.replace('21','1')
            else: #opt
                pass
        elif broker=='XDCE':
            if len(code)<=7: #fut
                pass
            else: #opt
                pass
        elif broker=='XINE':
            code=code.lower()
            
    
    return code

def getHtml(df,align='center', width='100%',classesName='tbl'):
    #d=df.copy()
    pd.set_option('display.max_colwidth', -1)
    #df.style.set_properties(**{'text-align': '%s'%(align)})
    
    html=df.to_html(classes=str(classesName))
    html=html.replace('<table border="1" class="dataframe %s"'%(classesName),'<table border="1" class="dataframe %s" width="%s"'%(classesName,width))
    html=html.replace('<tbody>','<tbody style="text-align: %s;">'%(align))
    
    return html

if __name__ == '__main__':
    code_jq2wxy('AG2105','XSGE')
    

